<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/css/public.css" media="all">
    <style>
        img {
            width: 100px;
            height: 60px;
        }
    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">姓名</label>
                            <div class="layui-input-inline">
                                <input type="text" name="name" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">职业</label>
                            <div class="layui-input-inline">
                                <input type="text" name="job_name" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary" lay-submit
                                    lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除</button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="bar"> 男女比例柱状图 </button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="jobPie"> 职业人数比例饼图 </button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="export"> 导出 </button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="import"> 导入 </button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="templatedownload"> 模板下载 </button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="detail">详情</a>
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>

    </div>
</div>
<script type="text/html" id="picTpl">
    <img src="{{d.pic}}" name="pic" alt="" style="width: 100%; height: 100%;">
</script>

<script src="../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;


        table.render({
            elem: '#currentTableId',
            url: 'employee/selectPage.do',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {type: "checkbox", width: 50},
                {field: 'id', title: 'ID', sort: true},
                {field: 'name', title: '姓名'},
                {
                    field: 'sex', title: '性别',width:50, templet: function (d) {
                        if (d.sex == 1) {
                            return '男';
                        } else {
                            return '女';
                        }
                    }
                },
                {field: 'phone', title: '手机号'},
                {field: 'birthday', title: '生日'},
                {field: 'create_time', title: '创建时间'},
                { field: 'pic', title: '照片', width: 150,height:200,templet: '#picTpl' },
                {title: '操作', minWidth: 100, toolbar: '#currentTableBar', align: "center"}
            ]],
            limit: 10,
            page: true,
            skin: 'line'
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            var result = JSON.stringify(data.field);
            //执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , where: {
                    // searchParams: result,
                    "name": data.field.name,
                    "job_name": data.field.job_name
                }
            }, 'data');

            return false;
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            var checkStatus = table.checkStatus('currentTableId');
            var data = checkStatus.data;
            if (obj.event === 'delete') {  // 监听删除操作
                var arr = [];
                for (var i = 0; i < data.length; i++) {
                    arr.push(data[i].id);
                }
                var ids = arr.join(",");

                $.get("/employee/deleteByIds.do", {"ids": ids}, function (result) {
                    if (result.code == 200) {
                        layer.msg("删除成功");
                        // obj.del();
                        table.reload('currentTableId');
                    } else {
                        layer.msg("删除失败");
                    }

                }, "json")
            }else if(obj.event === 'bar'){
                layer.open({
                    title: '男女比例',
                    shade: 0.2,
                    type: 2,
                    shadeClose: true,   // 点别的地方 是否关闭当前 layer    true收关闭   false 不关闭
                    area: ['80%', '100%'],
                    fixed: true, //不固定
                    maxmin: true,
                    content: 'employeeSexBar.html',
                    end: function (index) {
                    }
                });
            }else if (obj.event === 'export'){
                location.href="/employee/export.do";
            }else if(obj.event === 'import'){
                layer.open({
                    title: '学员信息导入',
                    shade: 0.2,
                    type: 2,
                    shadeClose: true,   // 点别的地方 是否关闭当前 layer    true收关闭   false 不关闭
                    area: ['50%', '80%'],
                    fixed: true, //不固定
                    maxmin: true,
                    content: 'employeeImport.html',
                    end: function (index) {
                        table.reload('currentTableId');
                    }
                });
            }else if(obj.event === 'templatedownload'){
                location.href="/employee/templateDownload.do";
            }else if(obj.event === 'jobPie'){
                layer.open({
                    title: '职业人数比例',
                    shade: 0.2,
                    type: 2,
                    shadeClose: true,   // 点别的地方 是否关闭当前 layer    true收关闭   false 不关闭
                    area: ['80%', '100%'],
                    fixed: true, //不固定
                    maxmin: true,
                    content: 'employeeJobPie.html',
                    end: function (index) {
                    }
                });
            }
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            localStorage.setItem("employee", JSON.stringify(data));
            if (obj.event === 'edit') {

                var index = layer.open({
                    title: '编辑用户',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: 'employeeEdit.html',
                    end: function () {
                        table.reload('currentTableId');
                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('真的删除行么', function (index) {

                    $.get("/employee/delete.do?id=" + data.id, (res) => {
                        if (res.code == 200) {
                            //删除成功
                            obj.del();
                            layer.msg("删除成功！");
                            layer.close(index);
                        } else {
                            layer.msg("亲,删除失败！");
                        }
                    })


                });
            }else if(obj.event === 'detail'){
                location.href="employeeDetail.html";
            }
        });

    });
</script>

</body>
</html>